package com.blockmeta.bbs.overallserviceapplication.net;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.Button;

import com.blockmeta.bbs.baselibrary.net.callback.MyStringCallback;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by author on 2018/7/31.
 */

public class SmsEcodeHelper {
    private Context context;
    private Button btn;
    private CountDownTimer mDownTimer;
    private TimeEquals mTimeEquals;


    public TimeEquals getTimeEquals() {
        return mTimeEquals;
    }

    public void setTimeEquals(TimeEquals timeEquals) {
        mTimeEquals = timeEquals;
    }


    public SmsEcodeHelper(Context context, Button btn) {
        this.context = context;
        this.btn = btn;
        initCountTimer();
    }

    /**
     * 发送数据
     *
     * @param map
     */
    public void DealWithEcode(HashMap<String, String> map) {
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .content(new Gson().toJson(map))
                .url(NetConstant.SMS)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtil.show(context,"获取验证码失败哦");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ToastUtil.show(context,"发送验证码成功，请查收");
                    }
                });
    }

    /**
     * 倒计时开启关闭
     * 涉及到开启和关闭 的回调问题;
     */
    public void initCountTimer() {
        mDownTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long l) {
                btn.setEnabled(false);
                btn.setBackground(context.getResources().getDrawable(R.drawable.shape_main_bg_background_gray));
                btn.setTextColor(context.getResources().getColor(R.color.text_light_gray));
                btn.setText((l / 1000) + "秒");
                int time = (int) (l / 1000L);
                mTimeEquals.timeequals(time);

            }

            @Override
            public void onFinish() {
                btn.setEnabled(true);
                btn.setBackground(context.getResources().getDrawable(R.drawable.shape_main_button_bg));
                btn.setTextColor(context.getResources().getColor(R.color.bg_white));
                btn.setText("发送验证码");
            }

        };
    }


    public void setCount(CountDownTimerCallBack timerCallBack) {
        timerCallBack.countdowncallback(mDownTimer);
    }

    public interface CountDownTimerCallBack {
        void countdowncallback(CountDownTimer countDownTimer);
    }

    public interface TimeEquals {
        void timeequals(int time);
    }
    public void releaseCountDown(){
        if (mDownTimer!=null){
            mDownTimer.cancel();
        }
    }
}
